package com.my.spvideo.service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.my.spvideo.dao.mysql.baidumvslave.ChannelViewRealMapper;
import com.my.spvideo.dao.mysql.baidumvslave.ChannelincomSlaveMapper;
import com.my.spvideo.dao.mysql.stat.ChannelNewUserMapper;
import com.my.spvideo.enums.DateType;
import com.my.spvideo.enums.ShareType;
import com.my.spvideo.util.NumUtil;
import com.my.spvideo.vo.Channel;
import com.my.spvideo.vo.Channelincom;
import com.my.spvideo.vo.OrderStat;
import com.my.spvideo.vo.UserStat;

/**
 * 渠道收益查看
 * 
 * @author zhangcheng
 *
 */
@Service
public class ChanneViewComService {
	@Autowired
	private ChannelViewRealMapper channelViewRealMapper;
	@Autowired
	private ChannelincomSlaveMapper channelincomSlaveMapper;
	@Autowired
	private ChannelNewUserMapper channelNewUserMapper;
	// @Autowired
	// private ChannelMapper channelMapper;

	// 初始化
	public JSONObject getChannelincomlist(String startTime, String endTime, String channelId,
			List<Integer> companyIdList) {
		JSONObject json = null;
		LocalDate today = LocalDate.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDate endDate = LocalDate.parse(endTime, formatter);
		if (today.isEqual(endDate) && (channelId != null && !channelId.equals("-1"))) { // 当天数据
			json = channelincomThatDay(endTime, channelId, companyIdList);
		} else { // 历史数据 channelid sharetype sum(channelincom)
			json = channelincomHistory(startTime, endTime, channelId, companyIdList);
		}
		return json;

	}

	// 当天数据
	private JSONObject channelincomThatDay(String time, String channelId, List<Integer> companyIdList) {
		JSONObject json;
		Channel channel = channelViewRealMapper.getChannel(channelId, companyIdList); // channel表对应的渠道信息
		if (channel.getShareType() == ShareType.CPS.ordinal() &&channel.getDateType() == DateType.REAL_TIME.ordinal()) {
			Channelincom channelIncom = null;
			Channelincom total = new Channelincom();
			List<Channelincom> channelIncomList = channelViewRealMapper.getOrderPriceCount(channelId, time); // 统计渠道流水
			for(Channelincom temp : channelIncomList){
				if(temp.getDeductFlag()==0){
					channelIncom = temp;
				}
			}
			if (channelIncom == null) {
				return new JSONObject();
			}else{
				if(channel.getShowHundred()==0){
//					channelIncom.setIncome((int) (channelIncom.getPricecount() * channel.getShareRatio() / 100));
					channelIncom.setIncomeDisplay(NumUtil.mathFormat(channelIncom.getPricecount()*channel.getShareRatio()/100));
				}else{
//					channelIncom.setIncome((int)channelIncom.getPricecount());
					channelIncom.setIncomeDisplay(NumUtil.mathFormat(channelIncom.getPricecount()));

				}
			}
			List<String> channelIdList = new ArrayList<String>(1);
			channelIdList.add(channelId);
			List<UserStat> newUserCountList = channelNewUserMapper.selectNewUserCount(time, time, channelIdList);
			if (!newUserCountList.isEmpty()) {
				UserStat newUserCount = newUserCountList.get(0);
				if (newUserCount.getNewUserCount() >= 100) {
					channelIncom.setUserCount((int) (newUserCount.getNewUserCount() * 0.95));
				} else {
					channelIncom.setUserCount(newUserCount.getNewUserCount());
				}
			}
			total.setUserCount(channelIncom.getUserCount());
//			total.setIncome(channelIncom.getIncome());
			total.setIncomeDisplay(channelIncom.getIncomeDisplay());
			List<Channelincom> list = new ArrayList<Channelincom>(1);
			list.add(channelIncom);
			json = new JSONObject();
			json.put("total", total);
			json.put("list", list);
			return json;
		}
		return new JSONObject();
	}

	// 历史数据 channelid sharetype sum(channelincom)
	private JSONObject channelincomHistory(String startTime, String endTime, String channelId,
			List<Integer> companyIdList) {
		// dateType=1 +渠道流水、分成比例
		List<OrderStat> orderStatList = channelincomSlaveMapper.getDateTypeRealList(startTime, endTime, companyIdList,
				channelId);
		TreeMap<String, Channelincom> channelIncomeMap = new TreeMap<String, Channelincom>();
		Channelincom total = new Channelincom();
		for (OrderStat orderStat : orderStatList) {
			Channelincom current = channelIncomeMap.get(orderStat.getChannelId());
			if (current == null) {
				current = new Channelincom();
				current.setChannelId(orderStat.getChannelId());
				current.setShareType(orderStat.getShareType());
				current.setUserCount(orderStat.getNewUserCountDedu()); // 扣量新增用户数
																		// 2016-11-03
				channelIncomeMap.put(current.getChannelId(), current);
			}
//			current.setIncome(current.getIncome() + orderStat.getChannelIncome());
//			total.setIncome(total.getIncome() + orderStat.getChannelIncome());
			current.setIncomeDisplay(NumUtil.mathFormat(current.getIncomeDisplay()+orderStat.getChannelIncome()));
			total.setIncomeDisplay(NumUtil.mathFormat(total.getIncomeDisplay()+orderStat.getChannelIncome()));
			total.setUserCount(total.getUserCount() + current.getUserCount());
		}
		// dateType=2 +流水和单价 /用户数分成比例
		// 2016-07-20 -流水和单价 /用户数分成比例
		List<Channelincom> dateTypeTwoList = channelincomSlaveMapper.getDateTypeNextList(startTime, endTime,
				companyIdList, channelId);
		for (Channelincom channelincom : dateTypeTwoList) {
			Channelincom current = channelIncomeMap.get(channelincom.getChannelId());
			if (current == null) {
				current = new Channelincom();
				current.setChannelId(channelincom.getChannelId());
				current.setShareType(channelincom.getShareType());
				channelIncomeMap.put(current.getChannelId(), current);
			}
			current.setIncomeDisplay(current.getIncomeDisplay()+channelincom.getIncome());
			// 只有cps才计算渠道收益总计
			if (channelincom.getShareType() == 2) {
				total.setIncomeDisplay(total.getIncomeDisplay()+channelincom.getIncome());
			}
			if (channelincom.getShareType() == 1) {
				current.setUserCount(channelincom.getUserCount());
//				current.setIncome(0);
				current.setIncomeDisplay(0);
//				total.setIncome(total.getIncome() + 0);
				total.setIncomeDisplay(total.getIncomeDisplay()+0);
			}

		}
		JSONObject json = new JSONObject();
		json.put("total", total);
		json.put("list", channelIncomeMap.values());
		return json;
	}

}
